# 导包
import pymysql
# 建立连接
try:
    conn = pymysql.connect(host="localhost",port=3306,user='root',password='root',database='books',charset="utf8")
    # 获取游标
    cursor = conn.cursor()
    sql_insert = "insert into t_book(id,title,`read`,`comment`,pub_date) values(4,'小明的哥哥叫大明',50,0,'2020-01-01');"
    cursor.execute(sql_insert)

    cursor.execute("select * from t_book where title='小明的哥哥叫大明';")
    print("小明插入的书：", cursor.fetchall())
    conn.commit()
except Exception as f:
    print(f)
    conn.rollback()
# 关闭游标
finally:
    cursor.close()
    # 关闭连接
    conn.close()

# # 输出
# 小明插入的书： ((8, '小明的哥哥叫大明', datetime.date(2020, 1, 1), 50, 0, 0),)
# # 通过数据库连接查询结果：
# 1	射雕英雄传	1980-05-01	12	34	0
# 2	天龙八部	1986-07-24	36	40	0
# 3	笑傲江湖	1995-12-24	20	80	0